//
//  SDVoiceTipView.m
//  for_test
//
//  Created by Carlson Lee on 2017/4/5.
//  Copyright © 2017年 Carlson Lee. All rights reserved.
//

#import "SDVoiceTipView.h"

@interface SDVoiceTipView ()
{
    TIP_TYPE _type;
    TIP_DERECTION _direction;
    UIFont* _font;
}
@property(nonatomic, strong)UIImageView* tipImg;


@end

@implementation SDVoiceTipView

- (instancetype)init
{
    if(self = [super init]){
        [self setBackgroundColor:AlphaColorWithRGBA(0x000000, .5)];
        _type = TIP_TYPE_STR;
        _direction = TIP_DERECTION_HOR;
        _font = SDFont(15, FONT_TYPE_REGULAR);
    }
    return self;
}

- (instancetype)initWithType:(TIP_TYPE )type direction:(TIP_DERECTION )direction font:(UIFont* )font
{
    if(self = [super init]){
        _type = type;
        _direction = direction;
        _font = font;
    }
    return self;
}

- (UIImageView *)tipImg
{
    if(!_tipImg){
        _tipImg = [[UIImageView alloc]init];
        [self addSubview:_tipImg];
    }
    return _tipImg;
}

- (UILabel *)tipLab
{
    if(!_tipLab){
        _tipLab = [[UILabel alloc]init];
        [self addSubview:_tipLab];
    }
    return _tipLab;
}

- (void)setImgStr:(NSString *)imgStr
{
    _imgStr = imgStr;
    self.tipImg.image = [UIImage imageNamed:_imgStr];
    
    [self setNeedsLayout];
}

- (void)setTipStr:(NSString *)tipStr
{
    _tipStr = tipStr;
    self.tipLab.text = _tipStr;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize sz = SDSize(_tipStr, _font, CGSizeZero);
    [self.tipLab setFont:_font];
    if(_type == TIP_TYPE_STR){
        if(_direction == TIP_DERECTION_HOR){
            self.tipLab.frame = CGRectMake((UIFrameW(self)-sz.width)/2, (UIFrameH(self)-sz.height)/2, sz.width, sz.height);
        }
    }else if (_type == TIP_TYPE_IMG_STR){
        CGSize imgSz = self.tipImg.image.size;
        if(_direction == TIP_DERECTION_HOR){
            self.tipImg.frame = CGRectMake(11*ScaleX, (UIFrameH(self)-imgSz.height)/2, imgSz.width, imgSz.height);
            self.tipLab.frame = CGRectMake(CGRectGetMaxX(self.tipImg.frame)+6*ScaleX, (UIFrameH(self)-sz.height)/2, sz.width, sz.height);
        }else{
            self.tipImg.frame = CGRectMake((UIFrameW(self)-imgSz.width)/2, (UIFrameH(self)-(imgSz.height+sz.height+11)*ScaleX)/2, imgSz.width, imgSz.height);
            self.tipLab.frame = CGRectMake((UIFrameW(self)-sz.width)/2, CGRectGetMaxY(self.tipImg.frame)+11*ScaleX, sz.width, sz.height);
        }
    }else{
    
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if(_delegate && [_delegate respondsToSelector:@selector(sdVoiceTipViewSelect)]){
        [_delegate sdVoiceTipViewSelect];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
